﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using RN.ERP.Web.OrderService;
using Newtonsoft.Json;
using System.Web.UI.HtmlControls;
using RN.ERP.Web.EnterpriseService;
using RN.ERP.Web.ModelService;
using System.Web.Services;
using Fly.Common.Linq;

namespace RN.ERP.Web.CA
{
    public partial class CAOrderList : BasePage
    {
        OrderServiceClient client = new OrderServiceClient();
        EnterpriseServiceClient enterpriseClient = new EnterpriseServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            ////list.html
            if (!IsPostBack)
            {
                hidGroup.Value = Request.QueryString["group"];//产品组
                ModelService.Tb_Corp_CompanyInfo corp = new ModelService.Tb_Corp_CompanyInfo();
                corp = JsonConvert.DeserializeObject<ModelService.Tb_Corp_CompanyInfo>(enterpriseClient.GetCorpInfo(UserInfo.CorpUserGuid, UserInfo.CurrentArea));
                if (corp != null)
                {
                    hidCorpName.Value = corp.CorpName;
                }
                addOrder.HRef = "CAOrderDetail.aspx?group=" + hidGroup.Value;
            }
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            RadGrid1.AllowCustomPaging = true;
            int pageCount = RadGrid1.CurrentPageIndex;
            int pageSize = RadGrid1.PageSize;
            List<QueryInvoiceInfoCondition> queryList = new List<QueryInvoiceInfoCondition>();
            QueryInvoiceInfoCondition queryCondition = new QueryInvoiceInfoCondition();
            if (!string.IsNullOrEmpty(hidGroup.Value))
            {
                queryCondition.GroupCode = new ParameterValue<string>() { V = hidGroup.Value, O = OptEnum.Equal };
            }
            queryList.Add(queryCondition);
            RadGrid1.VirtualItemCount = client.GetOrderCount(JsonConvert.SerializeObject(queryList), Guid.Parse(UserInfo.CorpUserGuid));
            RadGrid1.DataSource = JsonConvert.DeserializeObject<List<ModelService.CaPayRelation>>(client.GetOrders(JsonConvert.SerializeObject(queryList), Guid.Parse(UserInfo.CorpUserGuid), pageCount, pageSize));
            //if (!string.IsNullOrEmpty(hidGroup.Value))
            //{
            //    RadGrid1.Columns[9].Visible = false;
            //}
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                e.Item.Cells[4].Text = hidCorpName.Value;
            }
            string str = "";
            int index = 0;
            int intRowSpan = 1;
            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                if (i == index)
                {
                    HiddenField lbId = (HiddenField)RadGrid1.Items[i].Cells[11].FindControl("hdnInvoice");
                    str = lbId.Value;
                    for (int j = i + 1; j < RadGrid1.Items.Count; j++)
                    {
                        HiddenField lbId2 = (HiddenField)RadGrid1.Items[j].Cells[11].FindControl("hdnInvoice");
                        if (lbId2.Value == str && !string.IsNullOrEmpty(str))
                        {
                            intRowSpan++;
                            RadGrid1.Items[i].Cells[11].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[11].Visible = false;
                            index++;
                        }
                        else if (j == RadGrid1.Items.Count - 1)
                        {
                            intRowSpan = 1;
                            index++;
                        }
                    }
                }
            }
        }

        public string GetInvoiceUrl(object oInvoiceGuid, object type)
        {
            if (type.ToString() == "1")
            {
                return string.Format("return ShowForm(\"CAOrderInvoice.aspx?o={0}&t=1\");", oInvoiceGuid);
            }
            else
            {
                return string.Format("return ShowAddForm(\"CAOrderInfo.aspx?group={0}&guid={1}\");", type, oInvoiceGuid);
            }
        }

        [WebMethod]
        public static string DelOrders(object keyguids)
        {
            string ret = null;
            OrderServiceClient orderclient = new OrderServiceClient();
            string[] guids = keyguids.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            OperationResult result = new OperationResult();
            for (int i = 0; i < guids.Length; i++)
            {
                result = JsonConvert.DeserializeObject<ModelService.OperationResult>(orderclient.DeleteOrder(Guid.Parse(guids[i])));
                if (!result.Result)
                {
                    break;
                }
            }
            ret = JsonConvert.SerializeObject(new { Message = result.Message });
            return ret;
        }
    }
}